package com.halflife.framework.interceptor;

import freemarker.ext.beans.BeansWrapper;
import freemarker.template.TemplateHashModel;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;

/**
 * Created by zhaoyf on 2018/3/4.
 */
public class GlobleVariableInterceptor implements HandlerInterceptor {
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,  ModelAndView modelAndView) throws Exception {
        BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
        TemplateHashModel staticModels = wrapper.getStaticModels();
        TemplateHashModel navStatic =(TemplateHashModel)staticModels.get("com.halflife.comeElectricity.admin.NavStatic");
        if(modelAndView!=null){
            modelAndView.getModel().put("nav",navStatic);
            String path =  request.getRequestURI().replaceFirst(request.getContextPath(),"").substring(7).replaceAll("\\/",".");
            modelAndView.getModel().put("path",path);
        }
    }
}
